package com.yuehuanghun.admin.core.config.util;

import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.yuehuanghun.admin.core.domain.SysDept;
import com.yuehuanghun.admin.core.domain.SysUser;
import com.yuehuanghun.admin.core.mapper.SysDeptMapper;

@Component
public class DeptUtils {
	private static final Logger logger = LoggerFactory.getLogger(DeptUtils.class);
	private static SysDeptMapper sysDeptMapper;

	@Autowired
	private void init(SysDeptMapper sysDeptMapper) {
		DeptUtils.sysDeptMapper = sysDeptMapper;
	}
	
	private static final LoadingCache<Long, Optional<SysDept>> CACHE = CacheBuilder.newBuilder().maximumSize(2000).expireAfterAccess(24, TimeUnit.HOURS).build(new CacheLoader<Long, Optional<SysDept>>() {
		@Override
		public Optional<SysDept> load(Long key) throws Exception {
			return sysDeptMapper.findById(key);
		}
	});
	
	public static void removeCache(Long deptId) {
		if(deptId == null) {
			return;
		}
		CACHE.invalidate(deptId);
	}
	
	public static void refreshCache(Long deptId) {
		if(deptId == null) {
			return;
		}
		CACHE.refresh(deptId);
	}
	
	public static String getDeptName(Long deptId) {
		if(deptId == null) {
			return null;
		}
		try {
			SysDept dept = CACHE.get(deptId).orElse(null);
			if(dept != null) {
				return dept.getDeptName();
			}
		} catch (Exception e) {
			logger.warn("获取dept缓存", e);
		}
		return null;
	}
	
	public static String getDeptName(Long deptId, String defaultValue) {
		String deptName = getDeptName(deptId);
		if(deptName == null) {
			return defaultValue;
		}
		return deptName;
	}

	public static String getDeptName(String deptId) {
		if (StringUtils.isBlank(deptId)) {
			return null;
		}

		if (StringUtils.isNumeric(deptId)) {
			return getDeptName(Long.valueOf(deptId));
		}
		return null;
	}
	
	public static String getDeptName(String deptId, String defaultValue) {
		String deptName = getDeptName(deptId);
		if(deptName == null) {
			return defaultValue;
		}
		return deptName;
	}
	
	public static String getDeptName(String deptId, Supplier<String> defaultValueSupplier) {
		String deptName = getDeptName(deptId);
		if(deptName == null) {
			return defaultValueSupplier.get();
		}
		return deptName;
	}
	
	public static SysDept getDept(Long deptId) {
		try {
			return CACHE.get(deptId).orElse(null);
		} catch (ExecutionException e) {
			logger.warn("获取dept缓存", e);
		}
		return null;
	}

	public static Long getDeptId(String userName) {
		SysUser user = UserUtils.getUser(userName);
		return user == null ? null : user.getDeptId();
	}

	public static Long getCompanyId(String userName) {
		SysUser user = UserUtils.getUser(userName);
		if (user == null) {
			return null;
		}

		SysDept dept = user.getDept();
		if (dept == null) {
			return null;
		}

		return StringUtils.isBlank(dept.getAncestors()) ? null : Long.valueOf(dept.getAncestors().split(",")[0]);
	}

}
